﻿#include "user-businessListPage.h"

BusinessListPage::BusinessListPage(database &db, QWidget *parent)
    : QWidget(parent), db(db)
{
    // 创建布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 10, 10, 10);

    // 创建标题
    QLabel *titleLabel = new QLabel("热门商户");
    titleLabel->setStyleSheet("font-size: 22pt; font-weight: bold; color: #409EFF; background: #fff; border-radius: 12px; padding: 18px 0 18px 24px; margin-bottom: 18px;");
    mainLayout->addWidget(titleLabel);

    // 创建刷新按钮
    QPushButton *refreshBtn = new QPushButton("刷新", this);
    refreshBtn->setFixedWidth(80);
    connect(refreshBtn, &QPushButton::clicked, this, &BusinessListPage::refresh);
    mainLayout->addWidget(refreshBtn, 0, Qt::AlignRight);

    // 创建表格展示商户
    businessTable = new QTableWidget(0, 5, this); // 增加营业状态列
    businessTable->setHorizontalHeaderLabels({"头像", "商户名称", "地址", "营业状态", ""});

    // 设置表格样式
    businessTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    businessTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    businessTable->setAlternatingRowColors(true);
    businessTable->verticalHeader()->setVisible(false);
    mainLayout->addWidget(businessTable);
    loadBusinessList();
}

BusinessListPage::~BusinessListPage()
{
}

bool BusinessListPage::loadBusinessList()
{
    businessTable->clearContents();
    businessTable->setRowCount(0);
    businessTable->setHorizontalHeaderLabels({"头像", "商户名称", "地址", "营业状态", ""});
    // 查询商户信息
    QString sql = "SELECT b.business_id, b.business_name, a.address, b.status, b.img FROM "
                  "business b JOIN address a ON b.address_id = a.address_id";

    QSqlQuery query = db.exec(sql);
    int row = 0;

    while(query.next()) {
        int businessId = query.value(0).toInt();
        QString businessName = query.value(1).toString();
        QString address = query.value(2).toString();
        int status = query.value(3).toInt();
        QString imgPath = query.value(4).toString();

        businessTable->insertRow(row);

        // 头像单元格
        QLabel *imgLabel = new QLabel();
        QString defaultAvatar = ":/img/img/clm.png";
        QString showPath = (!imgPath.isEmpty() && QFile::exists(imgPath)) ? imgPath : defaultAvatar;
        QPixmap pixmap(showPath);
        if (!pixmap.isNull()) {
            pixmap = pixmap.scaled(80, 80, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            imgLabel->setPixmap(pixmap);
        } else {
            imgLabel->setText("无头像");
        }
        imgLabel->setScaledContents(true);
        imgLabel->setAlignment(Qt::AlignCenter);
        businessTable->setCellWidget(row, 0, imgLabel);

        // 商户名称
        QTableWidgetItem *nameItem = new QTableWidgetItem(businessName);
        nameItem->setTextAlignment(Qt::AlignCenter);
        nameItem->setFont(QFont("", 12, QFont::Bold));
        businessTable->setItem(row, 1, nameItem);

        // 地址
        QTableWidgetItem *addressItem = new QTableWidgetItem(address);
        addressItem->setTextAlignment(Qt::AlignCenter);
        addressItem->setFont(QFont("", 11));
        businessTable->setItem(row, 2, addressItem);

        // 营业状态
        QString statusText = (status == 1) ? "营业中" : "休息中";
        QTableWidgetItem *statusItem = new QTableWidgetItem(statusText);
        statusItem->setTextAlignment(Qt::AlignCenter);
        statusItem->setFont(QFont("", 11));
        businessTable->setItem(row, 3, statusItem);

        // 进入按钮
        QPushButton *enterButton = new QPushButton("进入商户");
        enterButton->setFont(QFont("", 11, QFont::Bold));
        enterButton->setMinimumHeight(40);
        enterButton->setProperty("row", row);
        enterButton->setProperty("businessId", businessId);
        enterButton->setProperty("businessName", businessName);
        connect(enterButton, &QPushButton::clicked, [this, row]() {
            onBusinessClicked(row);
        });

        QWidget *buttonWidget = new QWidget();
        QHBoxLayout *buttonLayout = new QHBoxLayout(buttonWidget);
        buttonLayout->addWidget(enterButton);
        buttonLayout->setContentsMargins(5, 5, 5, 5);
        businessTable->setCellWidget(row, 4, buttonWidget);

        // 设置行高
        businessTable->setRowHeight(row, 300);

        // 图片点击事件
        imgLabel->setProperty("row", row);
        imgLabel->installEventFilter(this);

        row++;
    }

    // 调整列宽
    businessTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
    businessTable->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
    businessTable->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
    businessTable->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Fixed);
    businessTable->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Fixed);
    businessTable->setColumnWidth(0, 300);
    businessTable->setColumnWidth(4, 250);

    // 设置表头字体
    QFont headerFont = businessTable->horizontalHeader()->font();
    headerFont.setPointSize(12);
    headerFont.setBold(true);
    businessTable->horizontalHeader()->setFont(headerFont);
    businessTable->horizontalHeader()->setMinimumHeight(40);

    // 美化主背景色
    this->setStyleSheet("background-color: #f7f9fb;");

    // 美化按钮
    for (int row = 0; row < businessTable->rowCount(); ++row) {
        QWidget *buttonWidget = businessTable->cellWidget(row, 4);
        if (buttonWidget) {
            QPushButton *btn = buttonWidget->findChild<QPushButton*>();
            if (btn) {
                btn->setStyleSheet(
                    "QPushButton {"
                    " background: #409EFF;"
                    " color: #fff;"
                    " border-radius: 10px;"
                    " font-size: 14pt;"
                    " font-weight: bold;"
                    " min-width: 120px;"
                    " min-height: 40px;"
                    "}"
                    "QPushButton:hover { background: #66B1FF; }"
                );
            }
        }
    }
    return true;
}

void BusinessListPage::refresh() {
    loadBusinessList();
}

// 事件过滤器实现图片点击
bool BusinessListPage::eventFilter(QObject *obj, QEvent *event) {
    if (event->type() == QEvent::MouseButtonRelease) {
        QLabel *label = qobject_cast<QLabel*>(obj);
        if (label) {
            bool ok;
            int row = label->property("row").toInt(&ok);
            if (ok) {
                onBusinessClicked(row);
                return true;
            }
        }
    }
    return QWidget::eventFilter(obj, event);
}

void BusinessListPage::onBusinessClicked(int row) {
    QString businessName = businessTable->item(row, 1)->text();
    int businessId = -1;

    // 尝试获取商户ID
    QPushButton *button = qobject_cast<QPushButton*>(businessTable->cellWidget(row, 4)->findChild<QPushButton*>());
    if (button) {
        businessId = button->property("businessId").toInt();
    }

    // 发送商户选择信号
    emit businessSelected(businessId, businessName);
} 
